package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    //注入远程调用接口
    @Autowired
    private EduClient eduClient;
    @Autowired
    private UcenterClient ucenterClient;

    //        创建订单号
    //采用远程调用 的方式查询课程信息，和用户信息
    @Override
    public String saveOrders(String courseId, String memberIdByJwtToken) {

        //用户
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberIdByJwtToken);
        //课程
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);


        //取值
        //创建order对象  设置用户信息值，课程信息值
        Order order = new Order();

        order.setOrderNo(OrderNoUtil.getOrderNo());// 订单号  随机位置
        order.setCourseId(courseId);               //课程id
        order.setCourseTitle(courseInfoOrder.getTitle());    //标题
        order.setCourseCover(courseInfoOrder.getCover());     //封面
        order.setTeacherName(courseInfoOrder.getTeacherName()); //
        order.setTotalFee(courseInfoOrder.getPrice());
        order.setMemberId(memberIdByJwtToken);                //用户id
        order.setMobile(userInfoOrder.getMobile());
        order.setNickname(userInfoOrder.getNickname());

        order.setStatus(0);   //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1

        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }
}
